﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PasswordStoreProvider;
using Common.Exceptions;

namespace PasswordLocker.Controls
{
    public delegate void LoginInfoFormOKEvent(LoginInfoForm loginInfoForm);

    public partial class LoginInfoForm : Form
    {
        public event LoginInfoFormOKEvent OKClicked;
        public LoginInfoForm()
        {
            InitializeComponent();

            resourceLabel.Text = Resources.UrlResourceText;
            resourceTextBox.Tag = resourceLabel.Text;

            loginLabel.Text = Resources.LoginText;
            loginTextBox.Tag = loginLabel.Text;

            passwordLabel.Text = Resources.PasswordText;
            passwordTextBox.Tag = passwordLabel.Text;

            confirmPasswordLabel.Text = Resources.ConfirmPasswordButtonText;
            confirmPasswordTextBox.Tag = confirmPasswordLabel.Text;

            okButton.Text = Resources.OKButtonText;
            cancelButton.Text = Resources.CancelButtonText;

            Text = Resources.AppName;
        }

        public LoginInfo LoginInfo
        {
            get
            {
                return new LoginInfo { 
                            Login = loginTextBox.Text, 
                            Resource = resourceTextBox.Text };
            }
            set
            {
                // update login info and resource 
                loginTextBox.Text = value.Login;
                resourceTextBox.Text = value.Resource;

                // clear out the password fields
                passwordTextBox.Text = string.Empty;
                confirmPasswordTextBox.Text = string.Empty;
            }
        }

        public string Password
        {
            get
            {
                return passwordTextBox.Text;
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            // close the dialog box
            Close();
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            // validate input before proceeding
            if (!ValidateChildren())
            {
                return;
            }

            if(OKClicked != null)
            {
                OKClicked(this);
            }
        }

        /// <summary>
        /// Validates that the resource string is not empty. There is no requirement on it being
        /// valid
        /// </summary>
        private void ValidateInput(object sender, CancelEventArgs e)
        {
            var textBox = sender as TextBox;
            if(textBox == null)
            {
                return;
            }
            if(string.IsNullOrEmpty(textBox.Text))
            {
                e.Cancel = true;
                errorProvider.SetError(textBox,
                    string.Format(Resources.EmptyStringError, textBox.Tag as string));
            }
            // If we are validating the password, check to ensure both entries match
            else if (textBox == passwordTextBox &&
                    !string.Equals(passwordTextBox.Text,
                             confirmPasswordTextBox.Text,
                             StringComparison.CurrentCulture))
            {
                e.Cancel = true;
                errorProvider.SetError(passwordTextBox,
                                       Resources.PasswordMismatchError);
                errorProvider.SetError(confirmPasswordTextBox,
                                       Resources.PasswordMismatchError);
            }
        }
    }
}
